//# sourceURL=http://uMapApp/Module/StakeNumDataModule
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "./Module",
    "../UI/Panel/FloatPanel"
    , "../UI/Panel/PanelBlock/ListBlock"
    , "../UI/Panel/PanelBlock/PropertiesBlock"
    , "../UI/Panel/Item/ListItem"
    , "../UI/QueryNode"
    , "../UI/Layer"
    , "./StakeNum/Mileage"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel,
    ListBlock,
    PropertiesBlock,
    ListItem,
    QueryNode,
    Layer,
    Mileage
) {

    function StakeNumDataModule(option) {

        var _this = this;

        _this.panels = [];
        _this.items = [];

        Module.call(_this, option);

        _this.listPanel = new FloatPanel({"class": "rightPanel", "showClose": true});
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock"});
        _this.listPanel.listBlock.title = _this.title;
        _this.listPanel.append(_this.listPanel.listBlock);

        _this.panels.push(_this.listPanel);

        _this.layer = new Layer({
            "app": _this.app
        });

        _this.layer.pointPrimitives = new uMap.PointPrimitiveCollection();
        _this.layer.primitives.add(_this.layer.pointPrimitives.object);

        _this.layer.labelPrimitives = new uMap.LabelCollection();
        _this.layer.primitives.add(_this.layer.labelPrimitives.object);

        _this.layer.billboardPrimitives = new uMap.BillboardCollection();
        _this.layer.primitives.add(_this.layer.billboardPrimitives.object);

        _this.app.ui.layout.right.append(_this.listPanel);

    }

    var _class = StakeNumDataModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"           : {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"     : {
            value: function (list) {

                var _this = this;

                $.each(list, function (i, item) {

                    _this.items.push(item);

                    if (_this.dic.title) {
                        item.title = item[_this.dic.title];
                    }

                    if (_this.dic.properties) {
                        item.propertiesBlock = new PropertiesBlock();
                        item.propertiesBlock.title = item.title;
                        item.propertiesBlock.map = _this.dic.properties;
                        item.propertiesBlock.properties = item;
                    }

                    if (_this.dic.stakeNum) {
                        item.stakeNum = new Mileage(item[_this.dic.stakeNum]);
                        if (_this.roadLineModule.getSampledRoadCenterPositions(item.stakeNum)) {
                            item.position = item.position || new uMap.Position(_this.roadLineModule.getSampledRoadCenterPositions(item.stakeNum).position);
                        }
                    }

                    if (item.position) {

                        item.label = _this.layer.labelPrimitives.add(new uMap.LabelOption({
                            position                  : item.position
                            , text                    : item.title
                            , font                    : "18px Microsoft Yahei"
                            , fillColor               : ["#fff"]
                            , outlineColor            : ["#000"]
                            , outlineWidth            : 5.0
                            , horizontalOrigin        : "CENTER"
                            , verticalOrigin          : "TOP"
                            , pixelOffset             : [0, 10]
                            , level                   : [0, 1e5]
                            , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }));

                        item.billboard = _this.layer.billboardPrimitives.add(new uMap.BillboardOption({
                            position                  : item.position
                            , image                   : _this.app.icons.getIconMarker(_this.icon)
                            , verticalOrigin          : "BOTTOM"
                            , level                   : [0, 1e5]
                            , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }));

                        item.point = _this.layer.pointPrimitives.add(new uMap.PointOption({
                            position                  : item.position
                            , pixelSize               : 5
                            , color                   : ["#ff0"]
                            , outlineColor            : ["#000"]
                            , outlineWidth            : 2
                            , level                   : [0, 1e5]
                            , disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }));

                        item.openPop = function () {
                            if (_this.app.pop.target !== item) {
                                _this.activeItem(item);
                                _this.app.pop.target = item;
                                _this.app.pop.content = item.propertiesBlock;
                                _this.app.pop.position = item.position;
                                _this.app.pop.open();
                            }
                        };

                        item.label.on("click", function () {
                            item.openPop();
                        });

                        item.point.on("click", function () {
                            item.openPop();
                        });

                        item.billboard.on("click", function () {
                            item.openPop();
                        });

                    }

                    item.listItem = new ListItem({
                        "title"   : item.title,
                        "subtitle": item.stakeNum.str,
                        "icon"    : _this.icon.base64,
                        "item"    : item
                    });

                    item.searchKey = item.title;
                    item.sortKey = item.stakeNum.distance;


                    item.listItem.on("click", function () {
                        _this.activeItem(item);
                    });

                    _this.listPanel.listBlock.add(item.listItem);

                });

                _this.listPanel.listBlock.filter = {
                    "param": "item.searchKey"
                };

                _this.listPanel.listBlock.sorter = {
                    "param": {
                        "value": "item.sortKey",
                        "label": "桩号"
                    }
                };
            }
        },
        "load"           : {
            value: function (data, callback) {

                var _this = this;

                if (!_this.isLoaded) {

                    var queryNode = _this.app.queryManager.add(new QueryNode({
                        "title"   : "[模块] : " + _this.title,
                        "modality": true
                    }));

                    queryNode.trigger("send");

                    if (_this.request && _this.roadLineModule) {

                        _this.roadLineModule.loadInit(function () {
                            _this.request.send(data, function (json) {
                                if (json.dic) {
                                    _this.dic = json.dic;
                                }
                                if (json.data && json.data.length > 0) {
                                    _this.data = json.data;
                                }

                                _this.isLoaded = true;
                                _this.trigger("load");
                                queryNode.trigger("loaded");
                                callback && callback();
                            });
                        });
                    }

                } else {
                    callback && callback();
                }
            }
        },
        "clear"          : {
            value: function () {
                var _this = this;
                _this._data = undefined;
            }
        },
        "open"           : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.load({}, function () {
                        _this.isOpened = true;
                        _this.layer.open();
                        _this.trigger("open");
                        _this.trigger("toggle");
                    });
                }
            }
        },
        "close"          : {
            value: function () {
                var _this = this;
                _this.closePanel(false);
                _this.unActiveAllItem();
                _this.layer.close();
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "openPanel"      : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "activeItem"     : {
            value: function (item) {
                var _this = this;
                _this.unActiveAllItem();
                item.listItem.addClass("active");
                item.listItem.parent().animate({"scrollTop": item.listItem.dom[0].offsetTop});
            }
        },
        "unActiveAllItem": {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.listItem.removeClass("active");
                });
            }
        }
    });

    return _class;

});